﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.UI;
using System.Xml.Linq;
using System.Xml;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for BlocManager
/// </summary>
public class BlocManager
{
    public enum BlocWidth
    {
        Small,
        Big
    }
    public enum BlocType
    { 
        Texte,
        Liens,
        Focus,
        Actus,
        Menu,
        ImgTxt
    }

    public static Control GetFocus(BlocWidth width, string SectionName, string title)
    {
        Panel pan = new Panel();
        Image img = new Image();        
        string mapLoc = HttpContext.Current.Request.MapPath("~/Content/" + SectionName + ".xml");
        XElement m = XElement.Load(mapLoc);
        XElement item = (from el in m.Elements("item")
                         where (string)el.Element("title") == title
                         select el).First();
        if (width == BlocWidth.Small)
            img.ImageUrl = item.Element("thumbnailimgurl").Value;
        else
            img.ImageUrl = item.Element("imgurl").Value;
        img.CssClass = "imgThumb";
        img.Attributes.Add("hspace", "10");
        img.Attributes.Add("align", "left");
        img.BorderWidth = 1;
        pan.Controls.Add(img);
        
        HyperLink lnk = new HyperLink();
        lnk.Text = item.Element("title").Value;
        if (width == BlocWidth.Big)
        {
            lnk.CssClass = "TitleH2";
        }
        else
        {
            lnk.CssClass = "TitleH3";
        }
        lnk.ToolTip = item.Element("title").Value;
        lnk.NavigateUrl = item.Element("url").Value;
        pan.Controls.Add(lnk);
        Label br = new Label();
        br.Text = "<br/>";
        pan.Controls.Add(br);

        if (width == BlocWidth.Big)
        {
            HyperLink lnk2 = new HyperLink();
            lnk2.Text = item.Element("subtitle").ToString();
            lnk2.CssClass = "TitleH3";
            lnk2.ToolTip = item.Element("subtitle").ToString();
            lnk2.NavigateUrl = item.Element("url").Value;
            pan.Controls.Add(lnk2);
            br = new Label();
            br.Text = "<br>";
            pan.Controls.Add(br);
        }

        
        Literal  lbl = new Literal();
        lbl.Text = "<div class='texte'>"+item.Element("resume").ToString()+"</div>";
        lbl.Mode = LiteralMode.PassThrough;
        pan.Controls.Add(lbl);
        br = new Label();
        br.Text = "<br clear='left'/>";
        pan.Controls.Add(br);
        return (Control)pan;
    }
    
    public static Control GetArticle(BlocWidth width, string SectionName,string title)
    {
        Panel pan = new Panel();        
        Image img = new Image();
        
        string mapLoc = HttpContext.Current.Request.MapPath("~/Content/" + SectionName + ".xml");
        XElement m = XElement.Load(mapLoc);
        XElement item = (from el in m.Elements("item")
                         where (string)el.Element("title") == title
                         select el).First();
        if (width == BlocWidth.Small)
            img.ImageUrl = item.Element("thumbnailimgurl").Value;
        else
            img.ImageUrl = item.Element("imgurl").Value;
        img.ToolTip = item.Element("imgalt").Value;
        img.BorderWidth = 1;
        img.CssClass = "imgThumb";
        img.Attributes.Add("align", "left");
        img.Attributes.Add("hspace", "10");
        pan.Controls.Add(img);
        Label br;
        
        HyperLink lnk = new HyperLink();
        lnk.Text = item.Element("title").Value;
        if (width == BlocWidth.Big)
        {
            lnk.CssClass = "TitleH2";
        }
        else 
        {
            lnk.CssClass = "TitleH3";
        }
        lnk.ToolTip = item.Element("title").Value;
        lnk.NavigateUrl = item.Element("url").Value;
        pan.Controls.Add(lnk);
        br = new Label();
        br.Text = "<br>";
        pan.Controls.Add(br);

        if (width == BlocWidth.Big)
        {
            HyperLink lnk2 = new HyperLink();
            lnk2.Text = item.Element("subtitle").ToString();
            lnk2.CssClass = "TitleH3";
            lnk2.ToolTip = item.Element("subtitle").ToString();
            lnk2.NavigateUrl = item.Element("url").Value;
            pan.Controls.Add(lnk2);
            br = new Label();
            br.Text = "<br clear='left'>";
            pan.Controls.Add(br);

        }
        Literal  lbl = new Literal();
        lbl.Text = "<div class='texte'>" + item.Element("resume").Value + "</div>";
        
        pan.Controls.Add(lbl);
        
        return (Control)pan;
    }

    public static Control GetArticle(BlocWidth width, string SectionName)
    {
        Panel pan = new Panel();
        Image img;

        string mapLoc = HttpContext.Current.Request.MapPath("~/Content/" + SectionName + ".xml");
        XElement m = XElement.Load(mapLoc);
        IEnumerable<XElement> item = from el in m.Elements("item") orderby m.Element("order")
                        select el;
        foreach (XElement el in item)
        {
           
            Panel panact = new Panel();
            if (el.Equals(item.First()))
            {panact.CssClass="display";}
            else
            {panact.CssClass="hidden";}
            img = new Image();
            img.CssClass = "imgThumb";
            if (width == BlocWidth.Small)
                img.ImageUrl = el.Element("thumbnailimgurl").Value;
            else
                img.ImageUrl = el.Element("imgurl").Value;
            img.ToolTip = el.Element("imgalt").Value;            
            img.Attributes.Add("align", "left");
            img.BorderWidth = 1;
            img.Attributes.Add("hspace", "10");
            panact.Controls.Add(img);
            HyperLink lnk = new HyperLink();
            lnk.Text = el.Element("title").Value;
            if (width == BlocWidth.Small)
                lnk.CssClass = "TitleH3";
            else
                lnk.CssClass = "TitleH2";
            lnk.ToolTip = el.Element("resume").Value;
            lnk.NavigateUrl = el.Element("url").Value;
            panact.Controls.Add(lnk);
            if (width == BlocWidth.Big)
            {
                Label br1 = new Label();
                br1.Text = "<br>";
                panact.Controls.Add(br1);
                HyperLink lnk2 = new HyperLink();
                lnk2.Text = el.Element("subtitle").ToString();
                lnk2.CssClass = "TitleH3";
                lnk2.ToolTip = el.Element("subtitle").ToString();
                lnk2.NavigateUrl = el.Element("url").Value;
                panact.Controls.Add(lnk2);
                

            }
            Label br = new Label();
            br.Text = "<br clear='left'>";
            panact.Controls.Add(br);
            
            if (width == BlocWidth.Big)
            {
                br = new Label();
                br.Text = "<br>";
                panact.Controls.Add(br);
            }
            pan.Controls.Add(panact);
        }                    
        
        
        return (Control)pan;
    }


    public static Control GetArticleSlider(BlocWidth width, string SectionName)
    {
        
        Literal l = new Literal();
        l.Text = "<ul>";
        string mapLoc = HttpContext.Current.Request.MapPath("~/Content/" + SectionName + ".xml");
        XElement m = XElement.Load(mapLoc);
        IEnumerable<XElement> item = from el in m.Elements("item")
                                     orderby m.Element("order")
                                     select el;
        Literal l2;
        foreach (XElement el in item)
        {
            l2 = new Literal();
            l2.Text = "<li><img border=1 hspace='10' align='left' class=imgThumb ";
            if (width == BlocWidth.Small)
                l2.Text += " src='" + el.Element("thumbnailimgurl").Value.Substring(2) + "'";
            else
                l2.Text += " src='" + el.Element("imgurl").Value.Substring(2) +"' ";
            l2.Text += "alt='" + el.Element("imgalt").Value + "'>";
            l2.Text += "<a href='"+el.Element("url").Value+"' title='"+el.Element("resume").Value.Replace("'"," ")+"' ";
            if (width == BlocWidth.Small)
                l2.Text += " class='TitleH3' >";
            else
                l2.Text += " class='TitleH2' >";
            l2.Text += el.Element("title").Value + "</a>";
                        
            if (width == BlocWidth.Big)
            {
                l2.Text += "<br>";
                l2.Text += "<a class='TitleH3' href='" + el.Element("url").Value + "' title='" + el.Element("subtitle").Value + "' >" + el.Element("subtitle").Value + "</a>";                                
            }
            l2.Text+="<br clear='left'>";            
            if (width == BlocWidth.Big)
            {
                l2.Text += "<br>";                            
            }
            l2.Text += "</li>";
            l.Text+= l2.Text;
        }
        l.Text += "<ul>";
       
        return (Control)l;
    } 



    public static Control GetImgtxt(BlocWidth width, string SectionName,string title,bool withLink)
    {
        Panel pan = new Panel();
        Image img = new Image();
        img.CssClass = "imgThumb";
        string mapLoc = HttpContext.Current.Request.MapPath("~/Content/" + SectionName + ".xml");
        XElement m = XElement.Load(mapLoc);
        XElement item = (from el in m.Elements("item") where (string)el.Element("title")==title 
                                         select el).First();        
        img.ImageUrl=item.Element("thumbnailimgurl").Value;      
        img.CssClass = "imgThumb";
        img.Attributes.Add("align", "left");
        img.Attributes.Add("hspace", "10");
        img.BorderWidth = 1;
        pan.Controls.Add(img);
        if (withLink)
        {
            HyperLink lnk = new HyperLink();
            lnk.Text = item.Element("title").Value;
            lnk.CssClass = "TitleH3";
            lnk.ToolTip = item.Element("subtitle").ToString();
            lnk.NavigateUrl = item.Element("url").Value;
            pan.Controls.Add(lnk);
            Label br = new Label();
            br.Text = "<br clear='left'>";
            pan.Controls.Add(br);
        }
        else
        {
            Literal  lbl = new Literal();
            lbl.Text = "<div class='texte'>" + item.Element("title").Value + "</div>";
        
            pan.Controls.Add(lbl);
        }
        
        return (Control) pan;
    }

    public static Control GetTexte(BlocWidth width, string SectionName,string title,bool withLink)
    {
        string mapLoc = HttpContext.Current.Request.MapPath("~/Content/" + SectionName + ".xml");
        XElement m = XElement.Load(mapLoc);
        XElement item = (from el in m.Elements("item")
                         where (string)el.Element("title") == title
                         select el).First();      
        if (withLink)
        {
            HyperLink lnk = new HyperLink();
            lnk.Text = item.Element("title").Value;
            lnk.CssClass = "links";
            lnk.ToolTip = item.Element("subtitle").ToString();
            lnk.NavigateUrl = item.Element("url").Value;
            return lnk;
        }
        else
        {
            HtmlGenericControl cnt = new HtmlGenericControl("div");
            cnt.InnerHtml = item.Element("content").ToString() ;  
            return cnt;
        }
        
    }
    
    public static Control GetMenu(BlocWidth width, string SectionName)
    {
        Panel pan = new Panel();
        string mapLoc = HttpContext.Current.Request.MapPath("~/Content/" + SectionName + ".xml");
        XElement m = XElement.Load(mapLoc);
        IEnumerable<XElement> menuxml = from el in m.Elements("item") select el;
                    
        foreach (XElement el in menuxml)
        {
            HyperLink lnk = new HyperLink();
            lnk.NavigateUrl = el.Element("url").Value;
            lnk.CssClass = "MenuItem";
            lnk.Attributes.Add("clear", "left");
            lnk.Text = el.Element("title").Value;
            pan.Controls.Add(lnk);
            Label br = new Label();
            br.Text = "<br>";
            pan.Controls.Add(br);
        }                    
        return (Control) pan;     
    }




    public static Control GetLinks(BlocWidth width, string SectionName)
    {
        Panel pan = new Panel();
        string mapLoc = HttpContext.Current.Request.MapPath("~/Content/" + SectionName + ".xml");
        XElement m = XElement.Load(mapLoc);
        IEnumerable<XElement> menuxml = from el in m.Elements("item") select el;

        foreach (XElement el in menuxml)
        {
            HyperLink lnk = new HyperLink();
            lnk.NavigateUrl = el.Element("url").Value;
            lnk.CssClass = "MenuItem";           
            lnk.Text = el.Element("title").Value;
            pan.Controls.Add(lnk);
            Label br = new Label();
            br.Text = "<br>";            
            pan.Controls.Add(br);

        }
        return (Control)pan;
    }


}